package com.uroad.lib.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;

import com.uroad.lib.R;

import java.lang.reflect.Field;

public class DateDialog extends Dialog {

    Context mContext;
    Button btnCancel, btnOK;
    DatePicker datePicker;
    DialogOnclick dialogOkClick = null;

    public DateDialog(Context context, final DialogOnclick dialogOkClick) {
        super(context, R.style.baseCustomDialog);
        mContext = context;
        this.dialogOkClick = dialogOkClick;
        View view = LayoutInflater.from(mContext).inflate(
                R.layout.dialog_date, null);
        datePicker = (DatePicker) view.findViewById(R.id.datePicker);

        btnCancel = (Button) view.findViewById(R.id.btnCancel);
        btnOK = (Button) view.findViewById(R.id.btnOK);
        btnOK.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (dialogOkClick != null) {
                    dialogOkClick.PerDialogclick(DateDialog.this,
                            datePicker.getYear(), datePicker.getMonth() + 1, datePicker.getDayOfMonth());
                } else {
                    hideDialog();
                }
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                hideDialog();
            }
        });
        setCanceledOnTouchOutside(false);
        setContentView(view);
    }

    /**
     * @param isHideDay 是否隐藏日，只显示年月
     **/
    public DateDialog(Context context, boolean isHideDay, final DialogOnclick dialogOkClick) {
        super(context, R.style.baseCustomDialog);
        mContext = context;
        this.dialogOkClick = dialogOkClick;
        View view = LayoutInflater.from(mContext).inflate(
                R.layout.dialog_date, null);
        datePicker = (DatePicker) view.findViewById(R.id.datePicker);
        if (isHideDay) {
            hidDay(datePicker);
        }

        btnCancel = (Button) view.findViewById(R.id.btnCancel);
        btnOK = (Button) view.findViewById(R.id.btnOK);
        btnOK.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (dialogOkClick != null) {
                    dialogOkClick.PerDialogclick(DateDialog.this,
                            datePicker.getYear(), datePicker.getMonth() + 1, datePicker.getDayOfMonth());
                } else {
                    hideDialog();
                }
            }
        });
        btnCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                hideDialog();
            }
        });
        setCanceledOnTouchOutside(false);
        setContentView(view);
    }

    private void hidDay(DatePicker mDatePicker) {
        Field[] datePickerfFields = mDatePicker.getClass().getDeclaredFields();
        for (Field datePickerField : datePickerfFields) {
            if ("mDaySpinner".equals(datePickerField.getName())) {
                datePickerField.setAccessible(true);
                Object dayPicker = new Object();
                try {
                    dayPicker = datePickerField.get(mDatePicker);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
                // datePicker.getCalendarView().setVisibility(View.GONE);
                ((View) dayPicker).setVisibility(View.GONE);
            }
        }
    }

    public void hideDialog() {
        this.dismiss();
    }

    public interface DialogOnclick {
        void PerDialogclick(Dialog dialog, int year, int month, int day);
    }
}
